//
//  NewsViewController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/8/27.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsListCell.h"
#import "NewsListModel.h"
#import "NewsDetailController.h"
@interface NewsViewController ()
@property (weak, nonatomic) IBOutlet UIButton *dynamicClassButton;// 动态类
@property (weak, nonatomic) IBOutlet UIButton *newsButton;// 新闻类
@property (weak, nonatomic) IBOutlet UIButton *announcementButton;// 公告类
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSString *type;// 分类 1动态 2新闻 3公告
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation NewsViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    self.type = @"1";
    self.page = 1;
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    [self requestNewsData];
}
- (void)setupUI{
    self.tableView.rowHeight = 90;
     self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestNewsData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)requestNewsData{
    [self.dataArray removeAllObjects];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"type"] = self.type;
    param[@"pageNumber"] = @1;
    [NetWorkTool POST:bulletinList param:param success:^(id dic) {
        self.dataArray = [NewsListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        self.page = 2;
        self.dataArray = [self.dataArray mutableCopy];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        if (self.dataArray.count<10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:NO];
}
- (void)loadMoreData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"type"] = self.type;
    param[@"pageNumber"] = @(self.page);

    [NetWorkTool POST:bulletinList param:param success:^(id dic) {
        NSMutableArray *modelArray = [NewsListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        if (modelArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        self.page++;
        [self.dataArray addObjectsFromArray:modelArray];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsListModel *model = self.dataArray[indexPath.row];
    NewsListCell *cell = [NewsListCell cellWithTableView:tableView];
    cell.model = model;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsListModel *model = self.dataArray[indexPath.row];
    NewsDetailController * vc = [[NewsDetailController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@/lsb/homeApi/bulletinDetails?id=%@",HOST_URL,model.id];
    vc.titleString = @"资讯详情";
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)dynamicClassButtonClick:(UIButton *)sender {
    self.dynamicClassButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.newsButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.announcementButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.type = @"1";
    self.page = 1;
    [self requestNewsData];
}

- (IBAction)newsButtonClick:(UIButton *)sender {
    self.newsButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.dynamicClassButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.announcementButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.type = @"2";
    self.page = 1;
    [self requestNewsData];
}
- (IBAction)announcementButtonClick:(UIButton *)sender {
    self.announcementButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.dynamicClassButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.newsButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.type = @"3";
    self.page = 1;
    [self requestNewsData];
}

@end
